<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    程序举例（指针） - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						程序举例（指针）
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/19 21:28 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<h2><a id="%E9%A2%98%E7%9B%AE%E4%B8%80" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>题目一</h2>
<p>编写函数，使用指针，实现字符串处理函数strlen()，strcpy()，strcat()，strcmp()的功能</p>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;
#define N 10

int mystrlen(char *);           //测试字符串长度（返回一个整数）
int mystrcmp(char *, char *);   //比较字符串a和字符串b（返回1,0,-1）

char *mystrcpy(char *, char *); //将字符串b复制到字符数组a（返回字符串指针）
char *mystrcat(char *, char *); //将字符数组b连接到字符数组a后面（返回字符串指针）

int main()
{
    char s[N] = &quot;abcd&quot;;
    char d[N] = &quot;123&quot;;

    char *a = s;
    char *b = d;

    printf(&quot;%s %s\n&quot;, a, b);
    printf(&quot;strlen = %d\n&quot;, mystrlen(a));
    printf(&quot;strcmp = %d\n&quot;, mystrcmp(a, b));
    printf(&quot;strcpy = %s\n&quot;, mystrcpy(a, b));
    printf(&quot;strcat = %s\n&quot;, mystrcat(a, b));

    return 0;
}

//测试字符串长度（返回一个整数）
int mystrlen(char *a)
{
    char *q = a;
    while (*q != '\0')
        q++;
    return q - a;
}

//比较字符串a和字符串b（返回1,0,-1）
int mystrcmp(char *a, char *b)
{
    char *pa = a, *pb = b;
    while (*pa != '\0' || *pb != '\0')
    {
        if (*pa &gt; *pb || (*pa != '\0' &amp;&amp; *pb == '\0')) //当pa大于pb或pa不为'\0'，pb为'\0'时，返回1
        {
            return 1;
        }
        else if (*pa &lt; *pb || (*pa == '\0' &amp;&amp; *pb != '\0')) //当pa小于pb或pa为'\0'，pb不为'\0'时，返回-1
        {
            return -1;
        }
        pa++;
        pb++;
    }
    return 0;
}

//将字符串b复制到字符数组a（返回字符串指针）
char *mystrcpy(char *a, char *b)
{
    char *pa = a, *pb = b;
    while (*pb != '\0')
    {
        *pa = *pb;
        pa++;
        pb++;
    }
    *pa = '\0'; //复制时并没有将'\0'复制到对应的位置，需要添加'\0'
    return a;
}

//将字符数组b连接到字符数组a后面（返回字符串指针）
char *mystrcat(char *a, char *b)
{
    char *pa = a, *pb = b;
    while (*pa != '\0')
        pa++;
    while (*pb != '\0')
    {
        *pa = *pb;
        pa++;
        pb++;
    }
    *pa = '\0';
    return a;
}
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>